/*     */ package com.zimbra.cs.util.tnef;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.util.tnef.mapi.TimeZoneDefinition;
/*     */ import java.io.IOException;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URISyntaxException;
/*     */ import java.text.DateFormat;
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Calendar;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.Iterator;
/*     */ import net.fortuna.ical4j.data.ContentHandler;
/*     */ import net.fortuna.ical4j.data.ParserException;
/*     */ import net.fortuna.ical4j.model.DateTime;
/*     */ import net.fortuna.ical4j.model.Parameter;
/*     */ import net.fortuna.ical4j.model.ParameterList;
/*     */ import net.fortuna.ical4j.model.Property;
/*     */ import net.fortuna.ical4j.model.parameter.Value;
/*     */ import net.freeutils.tnef.RawInputStream;
/*     */ import net.freeutils.tnef.TNEFUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IcalUtil
/*     */ {
/*  47 */   static Log sLog = ZimbraLog.tnef;
/*     */   
/*     */   public static void addProperty(ContentHandler icalOutput, Property icalProp) throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/*  51 */     if (icalProp == null) {
/*  52 */       return;
/*     */     }
/*  54 */     icalOutput.startProperty(icalProp.getName());
/*  55 */     icalOutput.propertyValue(icalProp.getValue());
/*  56 */     ParameterList plist = icalProp.getParameters();
/*  57 */     Iterator<?> it; if (plist != null) {
/*  58 */       for (it = plist.iterator(); it.hasNext();) {
/*  59 */         Object currObj = it.next();
/*  60 */         if ((currObj instanceof Parameter)) {
/*  61 */           Parameter parameter = (Parameter)currObj;
/*  62 */           icalOutput.parameter(parameter.getName(), parameter.getValue());
/*     */         }
/*     */       }
/*     */     }
/*  66 */     icalOutput.endProperty(icalProp.getName());
/*     */   }
/*     */   
/*     */   public static void addProperty(ContentHandler icalOutput, String propName, String propValue, boolean emptyIsOk) throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/*  71 */     if ((!emptyIsOk) && ((propValue == null) || (propValue.equals("")))) {
/*  72 */       return;
/*     */     }
/*  74 */     icalOutput.startProperty(propName);
/*  75 */     if (propValue == null) {
/*  76 */       icalOutput.propertyValue("");
/*     */     } else {
/*  78 */       icalOutput.propertyValue(propValue);
/*     */     }
/*  80 */     icalOutput.endProperty(propName);
/*     */   }
/*     */   
/*     */   public static void addProperty(ContentHandler icalOutput, String propName, Object propValue, boolean emptyIsOk) throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/*  85 */     if (propValue == null) {
/*  86 */       addProperty(icalOutput, propName, "", emptyIsOk);
/*     */     } else {
/*  88 */       addProperty(icalOutput, propName, propValue.toString(), emptyIsOk);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void addProperty(ContentHandler icalOutput, String propName, String propValue) throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/*  94 */     icalOutput.startProperty(propName);
/*  95 */     icalOutput.propertyValue(propValue);
/*  96 */     icalOutput.endProperty(propName);
/*     */   }
/*     */   
/*     */   public static String iCalDateTimeValue(DateTime actualTime, java.util.TimeZone javaTZ, boolean useDateOnly)
/*     */   {
/* 101 */     if (!useDateOnly) {
/* 102 */       return actualTime.toString();
/*     */     }
/* 104 */     String DATE_PATTERN = "yyyyMMdd";
/* 105 */     DateFormat dateOnlyFormat = new SimpleDateFormat(DATE_PATTERN);
/* 106 */     Calendar actualCalendar = new GregorianCalendar(javaTZ);
/* 107 */     actualCalendar.setTimeInMillis(actualTime.getTime());
/* 108 */     dateOnlyFormat.setCalendar(actualCalendar);
/* 109 */     return dateOnlyFormat.format(actualTime);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String icalUtcTime(long localTimeSince1601, TimeZoneDefinition tzDef)
/*     */   {
/* 120 */     DateTime localTime = localMinsSince1601toDate(localTimeSince1601, tzDef);
/*     */     
/* 122 */     SimpleDateFormat utcTimeFmt = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
/* 123 */     java.util.TimeZone utcTZ = net.fortuna.ical4j.model.TimeZone.getTimeZone("Etc/UTC");
/* 124 */     utcTimeFmt.setTimeZone(utcTZ);
/* 125 */     return utcTimeFmt.format(localTime);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String friendlyLocalTime(long localTimeSince1601, TimeZoneDefinition tzDef)
/*     */   {
/* 135 */     DateTime localTime = localMinsSince1601toDate(localTimeSince1601, tzDef);
/*     */     
/* 137 */     SimpleDateFormat localTimeFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
/*     */     java.util.TimeZone tz;
/* 139 */     java.util.TimeZone tz; if (tzDef == null) {
/* 140 */       tz = net.fortuna.ical4j.model.TimeZone.getTimeZone("Etc/UTC");
/*     */     } else {
/* 142 */       tz = tzDef.getTimeZone();
/*     */     }
/* 144 */     localTimeFmt.setTimeZone(tz);
/* 145 */     return localTimeFmt.format(localTime);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void addPropertyFromUtcTimeAndZone(ContentHandler icalOutput, String propName, DateTime utcTime, TimeZoneDefinition tzd, boolean useDateOnly)
/*     */     throws ParserException, URISyntaxException, ParseException, IOException
/*     */   {
/* 165 */     if (utcTime == null) {
/* 166 */       return;
/*     */     }
/* 168 */     boolean useUtc = true;
/* 169 */     java.util.TimeZone javaTZ = null;
/* 170 */     DateTime localTime = utcTime;
/* 171 */     if (tzd != null) {
/* 172 */       net.fortuna.ical4j.model.TimeZone tz = tzd.getTimeZone();
/* 173 */       javaTZ = tz;
/* 174 */       if (!tz.getID().equals("Etc/UTC")) {
/* 175 */         useUtc = false;
/* 176 */         Calendar actualCalendar = new GregorianCalendar(tz);
/* 177 */         actualCalendar.setTimeInMillis(utcTime.getTime());
/* 178 */         localTime = new DateTime(actualCalendar.getTime());
/* 179 */         localTime.setTimeZone(tz);
/*     */       }
/*     */     } else {
/* 182 */       javaTZ = net.fortuna.ical4j.model.TimeZone.getTimeZone("Etc/UTC");
/*     */     }
/* 184 */     icalOutput.startProperty(propName);
/* 185 */     icalOutput.propertyValue(iCalDateTimeValue(localTime, javaTZ, useDateOnly));
/* 186 */     if (!useUtc) {
/* 187 */       icalOutput.parameter("TZID", javaTZ.getID());
/*     */     }
/* 189 */     if (useDateOnly) {
/* 190 */       icalOutput.parameter("VALUE", Value.DATE.getValue());
/*     */     }
/* 192 */     icalOutput.endProperty(propName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void addFloatingDateProperty(ContentHandler icalOutput, String propName, DateTime floatingDate)
/*     */     throws ParserException, URISyntaxException, ParseException, IOException
/*     */   {
/* 212 */     if (floatingDate == null) {
/* 213 */       return;
/*     */     }
/* 215 */     java.util.TimeZone javaTZ = null;
/* 216 */     javaTZ = net.fortuna.ical4j.model.TimeZone.getTimeZone("Etc/UTC");
/* 217 */     icalOutput.startProperty(propName);
/* 218 */     icalOutput.propertyValue(iCalDateTimeValue(floatingDate, javaTZ, true));
/* 219 */     icalOutput.parameter("VALUE", Value.DATE.getValue());
/* 220 */     icalOutput.endProperty(propName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static DateTime localMinsSince1601toDate(long minsSince1601, TimeZoneDefinition tzd)
/*     */   {
/* 230 */     long millisecs = minsSince1601 * 60L * 1000L;
/*     */     
/* 232 */     millisecs -= 11644473600000L;
/*     */     
/* 234 */     GregorianCalendar utcCalendar = new GregorianCalendar(java.util.TimeZone.getTimeZone("Etc/UTC"));
/*     */     
/* 236 */     utcCalendar.setTimeInMillis(millisecs);
/* 237 */     if ((tzd == null) || (tzd.getTimeZone().getID().equals("Etc/UTC"))) {
/* 238 */       return new DateTime(utcCalendar.getTime());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 243 */     GregorianCalendar localCalendar = new GregorianCalendar(tzd.getTimeZone());
/* 244 */     localCalendar.set(utcCalendar.get(1), utcCalendar.get(2), utcCalendar.get(5), utcCalendar.get(11), utcCalendar.get(12), utcCalendar.get(13));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 250 */     DateTime localTime = new DateTime(localCalendar.getTime());
/* 251 */     return localTime;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Long readI32(RawInputStream ris)
/*     */     throws IOException
/*     */   {
/* 261 */     return Long.valueOf((ris.readU8() | ris.readU8() << 8 | ris.readU8() << 16 | ris.readU8() << 24) & 0xFFFFFFFFFFFFFFFF);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String readString(RawInputStream ris, int len, String oemCodePage)
/*     */     throws IOException
/*     */   {
/* 273 */     String myString = null;
/* 274 */     if ((oemCodePage != null) && 
/* 275 */       (oemCodePage.equals("Cp932"))) {
/* 276 */       oemCodePage = new String("Cp942");
/*     */     }
/*     */     
/* 279 */     byte[] asBytes = ris.readBytes(len);
/*     */     try {
/* 281 */       myString = TNEFUtils.removeTerminatingNulls(new String(asBytes, oemCodePage));
/*     */     } catch (UnsupportedEncodingException uee) {
/* 283 */       sLog.debug("Problem with oemCodePage=%s", oemCodePage, uee);
/* 284 */       myString = TNEFUtils.createString(asBytes, 0, len);
/*     */     }
/* 286 */     return myString;
/*     */   }
/*     */   
/*     */   public static String toHexString(byte[] bytes, int offset, int len) {
/* 290 */     if (bytes == null) {
/* 291 */       return "";
/*     */     }
/* 293 */     StringBuffer s = new StringBuffer();
/* 294 */     for (int i = 0; i < len; i++) {
/* 295 */       String b = Integer.toHexString(bytes[(offset + i)] & 0xFF).toUpperCase();
/* 296 */       if (b.length() == 1) {
/* 297 */         s.append('0');
/*     */       }
/* 299 */       s.append(b);
/*     */     }
/* 301 */     return s.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/tnef/IcalUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */